package com.eastfair.certificate.controller;

import com.eastfair.certificate.dto.FormFieldDTO;
import com.eastfair.certificate.entity.FormField;
import com.eastfair.certificate.entity.FormFieldGroup;
import com.eastfair.certificate.dto.FormFieldGroupSaveDTO;
import com.eastfair.certificate.dto.FormFieldGroupUpdateDTO;
import com.eastfair.certificate.dto.FormFieldGroupPageQuery;
import com.eastfair.certificate.service.FormFieldGroupService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.certificate.service.FormFieldService;
import com.eastfair.certificate.vo.FormFieldGroupVO;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 动态表单组
 * </p>
 *
 * @author ligang
 * @date 2023-05-17
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/formFieldGroup")
@Api(value = "FormFieldGroup", tags = "动态表单组")
@PreAuth(replace = "certificate:formFieldGroup:")
public class FormFieldGroupController extends SuperController<FormFieldGroupService, Long, FormFieldGroup, FormFieldGroupPageQuery, FormFieldGroupSaveDTO, FormFieldGroupUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    private FormFieldService formFieldService;


    @Override
    public void handlerResult(IPage<FormFieldGroup> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<FormFieldGroup> formFieldGroupList = list.stream().map((map) -> {
            FormFieldGroup formFieldGroup = FormFieldGroup.builder().build();
            //TODO 请在这里完成转换
            return formFieldGroup;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(formFieldGroupList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<FormFieldGroupPageQuery> params) {
        FormFieldGroupPageQuery pageQuery = params.getModel();

    }


    /**
     * 查询详情
     *
     * @param id 主键ID
     * @return
     */
    @ApiOperation(value = "查询详情", notes = "查询详情")
    @GetMapping("/findById")
//        @BaseControllerInfo(voGroup = {OrganizationVoGroupClass.findMenuOrFunctionById.class})
    public R<FormFieldGroupVO> findById(Long id) {
        FormFieldGroupVO formFieldGroupVO = baseService.findById(id);
        return R.success(formFieldGroupVO);
    }


    /**
     * 查询详情
     *
     * @return
     */
    @ApiOperation(value = "查询详情", notes = "查询详情")
    @GetMapping("/copy")
    public R copy() {
        FormFieldGroup formFieldGroup = baseService.getByIdCache(2011886309887709184L);
        FormFieldDTO formFieldDTO = new FormFieldDTO();
        formFieldDTO.setGroupId(formFieldGroup.getId());
        List<FormField> formFieldList = formFieldService.list(formFieldDTO,null);

        //复制
        FormFieldGroup newFormFieldGroup = new FormFieldGroup();
        BeanUtils.copyProperties(formFieldGroup,newFormFieldGroup);
        newFormFieldGroup.setId(null);
        newFormFieldGroup.setName(formFieldGroup.getName()+"-20220831");
        baseService.save(newFormFieldGroup);
        List<FormField> newFormFieldList = new ArrayList<>();
        for(FormField formField1 :formFieldList){
            FormField newFormField = new FormField();
            BeanUtils.copyProperties(formField1,newFormField);
            newFormField.setId(null);
            newFormField.setGroupId(newFormFieldGroup.getId());
            formFieldService.save(newFormField);
        }
        return R.success();
    }

}
